package geektime.concurrent.part2.concurrencycontrol;

import java.time.Instant;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

public class CyclicBarrierWorker implements Runnable {

    private CyclicBarrier barrier;

    public CyclicBarrierWorker(CyclicBarrier barrier) {
        this.barrier = barrier;
    }

    @Override
    public void run() {
        System.out.printf("[%s]已创建，阻塞在barrier\n", Thread.currentThread().getName());
        try {
            barrier.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
        System.out.printf("[%s]开始执行了，time=%s\n", Thread.currentThread().getName(), Instant.now());
    }
}
